import * as winston from 'winston';
require('winston-daily-rotate-file');
export interface IConfig {
  readonly env: string;
  readonly isProd: boolean;
  readonly baseDir: string;
  port: number;
  crossDomain: {
    allowedOrigins: string | string[];
    allowedReferer: string | string[];
  };
  typeOrm: any;
  winston: winston.LoggerOptions;
  logger: {
    level?: string;
    logDir?: string;
  };
  [propName: string]: any;
}

export default (config: IConfig) => {
  const { level, logDir } = config.logger;
  //   const transport = new winston.transports.DailyRotateFile({
  // dirnam
  //   });

  config.winston = {
    level,
    format: winston.format.combine(
      winston.format.colorize({
        colors: {
          info: 'blue',
          warn: 'yellow',
          debug: 'blue',
        },
      }),
      winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
      winston.format.printf((info): string => {
        if (info.stack) {
          info.message = info.stack;
        }
        return `${info.timestamp}[${info.pid} ${info.level}: ${
          info.context || 'Application'
        }] ${info.message}`;
      }),
    ),
    defaultMeta: { pid: process.pid },
    transports: [
      new winston.transports.Console(),
      new winston.transports.File({
        dirname: logDir,
        filename: 'error.log',
        level: 'error',
      }),
    ],
  };
  return config;
};
